home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / Interfaces / ODDebug.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  5.1 KB  |  165 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODDebug.h
  3.  
  4.     Contains:    Useful debugging macros and functions.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12.  
  13. #ifndef _ODDEBUG_
  14. #define _ODDEBUG_
  15.  
  16. #ifndef _ODTYPES_
  17. #include "ODTypes.h"
  18. #endif
  19.  
  20.  
  21. #ifdef _OD_IMPL_SHARE_UTILS_
  22. #pragma import on
  23. #endif
  24.  
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28.  
  29.  
  30. //=====================================================================================
  31. // ODInitExceptions
  32. //=====================================================================================
  33.  
  34. void ODInitExceptions( );
  35.  
  36.  
  37. //=====================================================================================
  38. // Debug Output
  39. //=====================================================================================
  40.  
  41. enum DebugOutputMode {
  42.     kNoOutput,
  43.     kWriteToFile,
  44.     kWriteToDebugWindow,
  45.     kGenerateDebugStrs
  46. };
  47.  
  48. DebugOutputMode    GetOutputMode( );
  49. void            SetOutputMode( DebugOutputMode );
  50.  
  51.  
  52. //=====================================================================================
  53. // Warnings
  54. //=====================================================================================
  55.  
  56. // WARN has the same syntax as printf but produces a SysBreakStr.
  57. // Warnings are disabled (and generate no code) when ODDebug is off.
  58.  
  59. #define WARN    if(!ODDebug) ; else _Warn
  60.  
  61.  
  62. //=====================================================================================
  63. // Assertions
  64. //=====================================================================================
  65.  
  66. // These all cause a debugger break if the condition evaluates to false or 0.
  67. // Leading "W" is a Warning: it doesn't raise an exception.
  68. // Trailing "M" means special Message displayed instead of condition.
  69.  
  70. #ifndef __MWERKS__
  71.     #define _FIL_ ""            /* MPW puts entire pathnames in; yuk! */
  72. #else
  73.     #define _FIL_ __FILE__
  74. #endif
  75.  
  76. #define ASSERT( COND, ERR )    \
  77.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR )
  78. #define ASSERTM( COND, ERR, MSG )    \
  79.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR, MSG )
  80. #define WASSERT( COND )    \
  81.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0)
  82. #define WASSERTM( COND, MSG )    \
  83.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0, MSG)
  84.  
  85. // ASSERT_NOT_NULL causes a debugger break and an exception if the parameter
  86. // is NULL. Use this in functions that take a pointer as a parameter but do
  87. // not allow the parameter to be NULL.
  88. // Do **NOT** use this macro to make sure memory allocation succeeded! It
  89. // has no effect in non-debug builds. Use THROW_IF_NULL instead.
  90.  
  91. #define ASSERT_NOT_NULL(PARAM) \
  92.     ASSERT((PARAM)!=kODNULL, kODErrIllegalNullInput)
  93.  
  94.  
  95. //=====================================================================================
  96. // Logging
  97. //=====================================================================================
  98.  
  99. // PRINT writes to the standard output via somPrintf if ODDebug is on. Use
  100. // SetOutputMode (or the ODDebug menu) to direct output to a file or to the
  101. // DebugWindow app.
  102.  
  103. #define PRINT    if(!ODDebug) ; else somPrintf
  104.  
  105. // LOG is like PRINT but can easily be turned on or off on a per-file basis.
  106. // To enable logging in a source file, you must redefine the symbol LOGGING
  107. // as "1" in that file, otherwise LOG statements will not be compiled. Make
  108. // sure to #undef the symbol before you re-#define it, as some compilers
  109. // won't redefine an already-defined symbol.
  110.  
  111. // PRINT and LOG statements do not generate any code if logging is off.
  112.  
  113. #define LOGGING 0        // Redefine as 1 in source file to enable logging
  114.  
  115. #define LOG        if(!ODDebug || !LOGGING) ; else somPrintf
  116.  
  117.  
  118. //==============================================================================
  119. // Safe Type-Casting
  120. //==============================================================================
  121.  
  122. /*    Use CAST as a safe way to cast a SOM object from one class to another.
  123.     For instance, if "o" is declared as an ODObject*, but your code knows
  124.     it's an ODPart*, you can say:
  125.             ODPart *part = CAST(o,ODPart);
  126.     If ODDebug is turned on, this will do a runtime check and cause an assertion
  127.     failure if o does not point to an ODPart (or subclass). Without ODDebug,
  128.     it degenerates into a simple C-style cast that generates no code.
  129.     
  130.     ASSERT_IS_A is similar to CAST but is used when you just want to assert
  131.     that the pointer points to the right kind of object.
  132. */
  133.  
  134. #if ODDebug
  135.     #define CAST(OBJ, CLASS)    ( (CLASS*)_Cast((OBJ), (somClassDataStructure*) &CLASS##ClassData,        \
  136.                                                         CLASS##_MajorVersion, CLASS##_MinorVersion) )
  137.     #define ASSERT_IS_A(OBJ,CLASS)    ( (void) CAST(OBJ,CLASS) )
  138. #else
  139.     #define CAST(OBJ, CLASS)    ( (CLASS*) (OBJ) )
  140.     #define ASSERT_IS_A(OBJ,CLASS)    /* */
  141. #endif
  142.  
  143.  
  144. //=====================================================================================
  145. // Internal goop...
  146. //=====================================================================================
  147.  
  148. void _Warn                ( char *fmt, ... );
  149. void _AssertionFailed    ( char *cond,  char *fileName,
  150.                             ODError, char *msg = kODNULL );
  151. #if ODDebug
  152. SOMObject* _Cast( SOMObject *obj, somClassDataStructure *cls, long major, long minor );
  153. #endif
  154.  
  155.  
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159.  
  160. #ifdef _OD_IMPL_SHARE_UTILS_
  161. #pragma import off
  162. #endif
  163.  
  164. #endif /*_ODDEBUG_*/
  165.